package com.sq.partner.manager.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.sq.partner.manager.CacheManager;
import com.sq.partner.manager.ConnectorManager;
import com.sq.partner.manager.LogManager;
import com.sq.partner.manager.MonitorManager;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.dao.impl.BaseDaoImpl;
import com.sq.partner.manager.util.ApplicationContextUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.StrUtil;

public class MonitorAppCache {
	private static Logger logger = LogUtil.getLog(MonitorAppCache.class);
	private static  List<MonitorManager> cacheMapList = new ArrayList<MonitorManager>();
	private static  List<MonitorManager> cacheMap = new ArrayList<MonitorManager>();

	private static   List<ConnectorManager> connectorList = new ArrayList<ConnectorManager>();
	private static  List<CacheManager> cacheList = new ArrayList<CacheManager>();
	private static  Map<String,String> serviceURL=  new HashMap<String,String>();
	
/**
 * 刷新缓存 需在系统初使化完成后使用
 * @return
 * @throws AppException 
 * @throws SQLException 
 */
	public static void refresh() throws AppException, SQLException{
		synchronized (cacheMapList) {
			cacheMapList.clear();
			connectorList.clear();
			cacheList.clear();
			serviceURL.clear();
			DataSource ds = (DataSource)ApplicationContextUtil.getBean("dataSource");
			if(ds == null){
				throw new AppException("无法获取数据源:"+ ds);
			}
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs = null;
			try{
				conn = ds.getConnection();
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select distinct APPID from  t_gateway_manager");
				while (rs.next()){
					MonitorManager monitorManager=getMonitors(rs.getString("APPID"));	
					if(StrUtil.isNotEmpty(monitorManager.getName())){
						cacheMapList.add(monitorManager);
					}
					
				}
			}finally{
				BaseDaoImpl.close(rs);
				BaseDaoImpl.close(stmt);
				BaseDaoImpl.close(conn);
			}
		}
	}
	private static MonitorManager getMonitors(String AppID)   throws AppException, SQLException {
		// TODO Auto-generated method stub
		DataSource ds = (DataSource)ApplicationContextUtil.getBean("dataSource");
		
		ConnectorManager connectorManager=null;
		CacheManager cacheManager=null;
		LogManager logManager=null;
	    MonitorManager  manitorManager= new MonitorManager();
		Map<String,String> serviceURL=  new HashMap<String,String>();
		String AppName="";
		if(ds == null){	
			throw new AppException("无法获取数据源:"+ ds);
		}
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement("select * from  T_GATEWAY_MANAGER where APPID=? ");
			pstmt.setString(1, AppID);
			rs = pstmt.executeQuery();
	
			while (rs.next()){
				serviceURL= getService(rs.getString("SERVICE_URL"));
				AppName= rs.getString("APPNAME");
				if("connectorManager".equals(rs.getString("MANAGER_NAME"))){
					connectorManager = new ConnectorManager();
					connectorManager.setName(rs.getString("OBJECT_NAME"));
					connectorManager.setObjectName(rs.getString("OBJECT_URL"));
					connectorManager.setServiceURL(serviceURL);
					connectorList.add(connectorManager);
				}
				if("cacheManager".equals(rs.getString("MANAGER_NAME"))){
					cacheManager = new CacheManager();
					cacheManager.setName(rs.getString("OBJECT_NAME"));
					cacheManager.setObjectName(rs.getString("OBJECT_URL"));
					cacheManager.setServiceURL(serviceURL);
					cacheList.add(cacheManager);
				}
				if("logManager".equals(rs.getString("MANAGER_NAME"))){
					logManager = new LogManager();
					logManager.setName(rs.getString("OBJECT_NAME"));
					logManager.setObjectName(rs.getString("OBJECT_URL"));
					logManager.setServiceURL(serviceURL);
				}
			}
			manitorManager.setAppId(AppID);
			manitorManager.setName(AppName);
			manitorManager.setConnectorManager(connectorList);
			manitorManager.setCacheManager(cacheList);
			manitorManager.setLogManager(logManager);
		}finally{
			BaseDaoImpl.close(rs);
			BaseDaoImpl.close(pstmt);
			BaseDaoImpl.close(conn);
		}
		return manitorManager;
	}
	public static List<MonitorManager> getMonitor(String AppID)   throws AppException, SQLException {
		// TODO Auto-generated method stub
		cacheMap.clear();
		connectorList.clear();
		cacheList.clear();
		serviceURL.clear();
		DataSource ds = (DataSource)ApplicationContextUtil.getBean("dataSource");
		MonitorManager  manitorManager= new MonitorManager();
		ConnectorManager connectorManager=null;
		CacheManager cacheManager=null;
		LogManager logManager=new LogManager();
		
		String AppName="";
		if(ds == null){	
			throw new AppException("无法获取数据源:"+ ds);
		}
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement("select * from  T_GATEWAY_MANAGER where APPID=? ");
			pstmt.setString(1, AppID);
			rs = pstmt.executeQuery();
	
			while (rs.next()){
				serviceURL= getService(rs.getString("SERVICE_URL"));
				AppName= rs.getString("APPNAME");
				if("connectorManager".equals(rs.getString("MANAGER_NAME"))){
					connectorManager = new ConnectorManager();
					connectorManager.setName(rs.getString("OBJECT_NAME"));
					connectorManager.setObjectName(rs.getString("OBJECT_URL"));
					connectorManager.setServiceURL(serviceURL);
					connectorList.add(connectorManager);
				}
				if("cacheManager".equals(rs.getString("MANAGER_NAME"))){
					cacheManager = new CacheManager();
					cacheManager.setName(rs.getString("OBJECT_NAME"));
					cacheManager.setObjectName(rs.getString("OBJECT_URL"));
					cacheManager.setServiceURL(serviceURL);
					cacheList.add(cacheManager);
				}
				if("logManager".equals(rs.getString("MANAGER_NAME"))){
					logManager = new LogManager();
					logManager.setName(rs.getString("OBJECT_NAME"));
					logManager.setObjectName(rs.getString("OBJECT_URL"));
					logManager.setServiceURL(serviceURL);
				}
			}
			manitorManager.setAppId(AppID);
			manitorManager.setName(AppName);
			manitorManager.setConnectorManager(connectorList);
			manitorManager.setCacheManager(cacheList);
			manitorManager.setLogManager(logManager);
			cacheMap.add(manitorManager);
		}finally{
			BaseDaoImpl.close(rs);
			BaseDaoImpl.close(pstmt);
			BaseDaoImpl.close(conn);
		}
		return cacheMap;
	}
	public static Map<String ,String> getService(String serviceURL)   throws AppException, SQLException {
		DataSource ds = (DataSource)ApplicationContextUtil.getBean("dataSource");
		Map<String ,String>    serviceMap =new  HashMap<String, String>();
		if(ds == null){	
			throw new AppException("无法获取数据源:"+ ds);
		}
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{

			conn = ds.getConnection();
			pstmt = conn.prepareStatement("select * from  t_gateway_jmx where SERVICE_URL=? ");
			pstmt.setString(1, serviceURL);
			rs = pstmt.executeQuery();
			while (rs.next()){
				serviceMap.put(rs.getString("SERVICE_KEY"), rs.getString("SERVICE_VALUE"));
			}
		}finally{
			BaseDaoImpl.close(rs);
			BaseDaoImpl.close(pstmt);
			BaseDaoImpl.close(conn);
		}
		return serviceMap ;
	}
	public static  List<MonitorManager> getCache(){
		if(cacheMapList.isEmpty()){
			try{
				refresh();
			}catch(Exception e){
				logger.error("初使化Monitor缓存失败",e);
			}
		}
		return cacheMapList;
		
	}

}
